package com.tgit.sso.paltform.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: StringUtil
 * @Description: 字符串工具类
 * @author fzj读你
 * @date 2019/2/26 15:05
 */
public class StringUtil {

    public static String ARR[] ="0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".toUpperCase().split(",");

    public static final String setToString(Set<String> set) {
        return setToString(set, ",");
    }

    public static final String setToString(Set<String> set, String delim) {
        String str = "";
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            str += iterator.next() + delim;
        }
        if (str.length() > 0) {
            str = str.substring(0, str.length() - delim.length());
        }
        return str;
    }

    public static final Set<String> stringToSet(String str, String delim) {
        if(str == null || str.length() == 0) {
            return null;
        }

        Set<String> set = new HashSet<>();
        String[] arr = str.split(delim);
        set.addAll(Arrays.asList(arr));
        return set;
    }

    /**
     * unicode转中文
     * @param str
     * @return
     */
    public static final String unicodeToString(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch+"" );
        }
        return str;
    }

    public static String genPwd(){
        String pwd="";
        for (int i = 0; i < 6; i++) {
            int index= (int) (Math.random()*ARR.length);
            pwd+=ARR[index];
        }
        return pwd;
    }

}
